package com.ihelp.rest.mis.users.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ihelp.rest.mis.platform.service.AdmdivisionService;
import com.ihelp.rest.mis.platform.service.UserService;
import com.ihelp.rest.mis.pojo.Users;
import com.ihelp.rest.mis.users.service.UsersService;
import com.ihelp.rest.utils.HtmlUtil;

/**
 * 会员管理
*      
* 类名称：UsersController   
* 类描述：     
* 创建时间：2013-3-21 下午02:34:46    
* 备注：   
* @version
 */
@Controller
@RequestMapping("/users")
public class UsersController {

	@Autowired
	private UsersService usersService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private AdmdivisionService admdivisionService;
	
	/**
	 * 加载会员主页面
	 * @param request
	 * @return
	 */
	@RequestMapping("users.do")
	public String loadUsersPage(HttpServletRequest request){
		return "users/users";
	}
	
	/**
	 * 查询省、市、县JSON 数据
	 * @param response
	 * @param parentcode
	 * @return
	 */
	@RequestMapping("findAdmdivision.do")
	public void findAdmdivision(HttpServletResponse response, String parentcode){
		HtmlUtil.writerJson(response, this.admdivisionService.findAdmdivision(parentcode));
	}
	
	/**
	 * 会员列表JSON数据
	 * @param response
	 * @param page
	 * @param rows
	 */
	@RequestMapping("findUsersPage.do")
	public void findUsersPage(HttpServletResponse response, int page, int rows){
		String json = this.usersService.findUsersPage(page, rows);
		HtmlUtil.writerJson(response, json);
	}
	
	/**
	 * 加载添加会员页面
	 * @param request
	 * @return
	 */
	@RequestMapping("loadAddUsersPage.do")
	public String loadAddUsersPage(HttpServletRequest request, String id){
		String sysUserJson = userService.findAllUser();
		Users users = this.usersService.getUsers(id);
		if(StringUtils.isNotBlank(sysUserJson)){
			sysUserJson = sysUserJson.replaceAll("\"", "\'");
		}
		request.setAttribute("SysUserJson", sysUserJson);
		request.setAttribute("Users", users);
		return "users/addUsers";
	}
	
	/**
	 * 会员保存
	 * @param response
	 * @param users
	 */
	@RequestMapping("addUsers.do")
	public void addUsers(HttpServletResponse response, Users users){
		String text = this.usersService.addUsers(users);
		HtmlUtil.writerText(response, text);
	}
	
}
